package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mall.dao.BrandDao;
import com.mall.dao.CategoryBrandRelationDao;
import com.mall.dao.CategoryDao;
import com.mall.entity.Brand;
import com.mall.entity.Category;
import com.mall.entity.CategoryBrandRelation;
import com.mall.service.BrandService;
import com.mall.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;
import org.springframework.util.StringUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public void saveDetail(CategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        Brand brand = brandDao.selectById(brandId);
        Category category = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(name);
        this.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelation>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategrory(catId,name);
//        Category category = new Category();
//        category.setCatId(catId);
//        category.setName(name);

//        this.update(category,new UpdateWrapper<Category>().eq("catelog_id",catId));


    }


    /**
     * 获取品牌id
     * @param catId
     * @return
     */
    @Override
    public List<Brand> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelation> categoryBrandRelations = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelation>().eq("catelog_id", catId));
        List<Brand> collect = categoryBrandRelations.stream().map(item -> {
            Long brandId = item.getBrandId();
            Brand byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

//    @Override //更新冗余字段
//    public void updateDetail(CategoryBrandRelation categoryBrandRelation) {
//        //先更新自己的
//        this.updateById(categoryBrandRelation);
//        if (!StringUtils.isEmpty())
//    }
//
//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<CategoryBrandRelationEntity> page = this.page(
//                new Query<CategoryBrandRelationEntity>().getPage(params),
//                new QueryWrapper<CategoryBrandRelationEntity>()
//        );
//
//        return new PageUtils(page);
//    }

}